package cn.dev.mybatis.core;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class MapperFactoryBean implements FactoryBean, InitializingBean, ApplicationContextAware {
    private Class<?> type;

    private SqlSession sqlSession;

    private ApplicationContext applicationContext;

    @Override
    public Object getObject() {
        return sqlSession.getMapper(type);
    }

    @Override
    public Class<?> getObjectType() {
        return type;
    }

    public Class<?> getType() {
        return type;
    }

    public void setType(Class<?> type) {
        this.type = type;
    }

    @Override
    public void afterPropertiesSet() {
        this.sqlSession = applicationContext.getBean(SqlSession.class);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }
}
